package com.xxxx.server.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sun.deploy.net.URLEncoder;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Ileave;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.service.IIleaveService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 辅导员离校报备 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/lea/ins/")
public class IleaveController {
    @Autowired
    private IIleaveService ileaveService;

    @ApiOperation(value = "获取所有辅导员请假信息")
    @GetMapping("/get/getall")
    public List<Ileave> getAllIleave(){
        return ileaveService.list();
    }

    @ApiOperation(value = "根据id获取辅导员请假信息")
    @GetMapping("/get/instrleave")
    public List<Ileave> getIleaveByIlId(){
        return ileaveService.getIleaveByIlId();
    }

    @ApiOperation(value = "添加辅导员请假信息")
    @PostMapping("/post/add")
    public RespBean addIleave(@RequestBody Ileave ileave){
        ileave.setCreatTime(LocalDateTime.now());
        ileave.setILid(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        if (ileaveService.save(ileave)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "更新辅导员请假信息")
    @PutMapping("/post/update")
    public RespBean updateIleave(@RequestBody Ileave ileave){
        ileave.setUpdateTime(LocalDateTime.now());
        if (ileaveService.updateById(ileave)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除辅导员请假信息")
    @DeleteMapping("/get/{id}")
    public RespBean deleteileave(@PathVariable Integer id){
        if (ileaveService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "批量删除辅导员请假信息")
    @DeleteMapping("/get/delete")
    public RespBean deleteIleaveByIds(Integer[] ids){
        if (ileaveService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "查询所有的辅导员请假（分页）")
    @GetMapping("/get/getPageIleave")
    public RespPageBean getInLeave(@RequestParam(defaultValue = "1") Integer currentPage ,
                                   @RequestParam(defaultValue = "10")Integer size,
                                   Ileave ileave,
                                   String[] beginDateScope){
        return ileaveService.getInleaveByPage(currentPage,size,ileave,beginDateScope);

    }

    //导出
    @ApiOperation(value = "导出辅导员请假数据")
    @GetMapping(value = "/get/export",produces = "application/octet-stream")
    public void exportEmployee(HttpServletResponse response){
        int adminid = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        List<Ileave> list = ileaveService.getIleave(null);
        ExportParams params = new ExportParams("辅导员请假","辅导员请假表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Ileave.class, list);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("辅导员请假表.xls","UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
